<template>
  <div class="app-container tz-app">
    <div class="zt-select flex-s">
      <div class="flex-left">
        <el-select v-model="queryParams.antennaName" filterable placeholder="天线" class="zt-input rihgt-m5">
          <el-option v-for="item in  dict.type.antenna_type" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>

        <el-select v-model="queryParams.antennaName" filterable placeholder="台站" class="zt-input-min rihgt-m5">
          <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
          </el-option>
        </el-select>
        <el-select v-model="queryParams.antennaName" filterable placeholder="电台" class="zt-input-min rihgt-m5">
          <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.label">
          </el-option>
        </el-select>
        <button class="bt-blue " @click="handleQuery">筛选</button>

      </div>
      <button class="bt-yellow " @click="handleAdd">新增</button>
      <!-- <el-input v-model="queryParams.antennaName" placeholder="请输入天线名称" class="zt-input" clearable
        @keyup.enter.native="handleQuery" /> -->

    </div>



    <div class="tz-table-box" style="height:calc(100vh - 250px);">
      <el-table v-loading="loading" :data="antennaList" @selection-change="handleSelectionChange"
        height="calc(100vh - 250px)" class="zt-table">
        <el-table-column label="天线ID" align="center" prop="antennaId" />
        <el-table-column label="天线名称" align="center" prop="antennaName" />
        <!-- <el-table-column label="天线编码" align="center" prop="antennaCode" /> -->
        <el-table-column label="天线类型" align="center" prop="antennaType" >
          <template slot-scope="scope">
            <!-- {{ dict.type.located_address[scope.row.locatedAddress].label }}  -->
            {{ getAntennaType(scope.row.antennaType ) }}
          </template>
        </el-table-column>
        <el-table-column label="所属位置" align="center" prop="locatedAddress">
          <template slot-scope="scope">
            <!-- {{ dict.type.located_address[scope.row.locatedAddress].label }}  getAntennaType-->
            {{ getLocatedAddress(scope.row.locatedAddress) }}
          </template>
        </el-table-column>
        <el-table-column label="所属节点" align="center" prop="locatedAddress">
          <template slot-scope="scope"> -</template>
        </el-table-column>
        <el-table-column label="所属电台" align="center" prop="locatedAddress">
          <template slot-scope="scope"> -</template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <div :class="scope.row.status == 1 ? 'red' : 'gray'">{{ scope.row.status == 1 ? '停用' : '正常' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px">
          <template slot-scope="scope">
            <div class="flex-s">
              <img src="@/assets/images/amend.png" style="width: 20px;height: 20px;cursor: pointer;margin-right: 20px;"
                @click="handleUpdate(scope.row)" />
              <img src="@/assets/images/delete-bin2.png" style="width: 20px;height: 20px;cursor: pointer;"
                @click="handleDelete(scope.row)" />
            </div>

            <!-- <el-button size="mini" type="text" icon="el-icon-edit" 
              v-hasPermi="['system:antenna:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" 
              v-hasPermi="['system:antenna:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

    </div>


    <div class="zt-pagination">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>



    <!-- 添加或修改天线对话框 -->

    <right-dialog :title="title" :visible="open" @closeDialog="cancel">
      <template v-slot:default>
        <div class="zt-dialog-solt">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="天线名称:" prop="antennaName">
              <div class="flex">
                <el-input class="blue-input" v-model="form.antennaName" placeholder="请输入天线名称" />
                <el-button class="bt-blue " @click="submitForm" style="margin-left: 10px; ">{{ title == '添加天线' ? '添加' :
                  '确定'
                }}</el-button>
              </div>
            </el-form-item>
            <el-form-item label="类型:">
              <div class="flex">
                <el-select v-model="form.antennaType" filterable placeholder="类型" class="zt-input-min rihgt-m5">
                  <el-option v-for="item in  dict.type.antenna_type" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>

                <!-- <div style="margin-right: 5px; width: 60px; text-align: right;font-weight: 600;">天线ID:
                </div>
                <el-input v-model="form.antennaCode" class="blank-input" placeholder="天线ID"
                  style="width: 120px;font-weight: 600;"></el-input> -->
                <div style="margin-right: 5px; width: 60px; text-align: right;font-weight: 600;">所属地:
                </div>
                <el-select v-model="form.locatedAddress" filterable placeholder="所属地" class="zt-input-min rihgt-m5"
                  style="width: 120px;font-weight: 600;">
                  <el-option v-for="item in  dict.type.located_address" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
                <div style=";margin-right: 5px; width: 60px; text-align: right;">状态:</div>
                <el-select v-model="form.status" filterable placeholder="状态" class="zt-input-min rihgt-m5">
                  <el-option key="0" label="正常" value="0" />
                  <el-option key="1" label="停用" value="1" />
                </el-select>

              </div>

            </el-form-item>
            <el-form-item label="备注:" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-form>

        </div>

      </template>
    </right-dialog>


  </div>
</template>

<script>
import { listAntenna, getAntenna, delAntenna, addAntenna, updateAntenna } from "@/api/business/antenna";
import RightDialog from "@/components/TZ/RightDialog"

export default {
  components: { RightDialog },
  name: "Antenna",
  dicts: ['antenna_type', 'located_address'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 天线表格数据
      antennaList: [],
      // 弹出层标题
      title: "",
      // 弹出类型
      openType: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        antennaCode: null,
        antennaType: null,
        locatedAddress: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        antennaName: [
          { required: true, message: "天线名称不能为空", trigger: "blur" }
        ],
        antennaCode: [
          { required: true, message: "天线编码不能为空", trigger: "blur" }
        ],
        antennaType: [
          { required: true, message: "天线类型不能为空", trigger: "change" }
        ],
        locatedAddress: [
          { required: true, message: "所属位置不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      },
      options: [{
        value: '1',
        label: '1'
      }, {
        value: '天线',
        label: '天线'
      }, {
        value: '2',
        label: '2'
      }, {
        value: '3',
        label: '3'
      }, {
        value: '天下七七',
        label: '天下七七'
      }],
      type: [{
        value: 0,
        label: '1'
      }, {
        value: 1,
        label: '天线'
      }, {
        value: 2,
        label: '2'
      }, {
        value: 3,
        label: '3'
      }, {
        value: 4,
        label: '天下七七'
      }]
    };
  },
  watch: {

  },
  created() {
    this.getList();
    console.log("1112331");
  },
  methods: {
    /** 查询天线列表 */
    getList() {
      this.loading = true;
      listAntenna(this.queryParams).then(response => {
        this.antennaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        antennaId: null,
        antennaName: null,
        antennaCode: null,
        antennaType: null,
        locatedAddress: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.dict.type);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.antennaId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加天线";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const antennaId = row.antennaId || this.ids
      getAntenna(antennaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑天线";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.antennaId != null) {
            updateAntenna(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAntenna(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const antennaIds = row.antennaId || this.ids;
      this.$modal.confirm('是否确认删除天线编号为"' + antennaIds + '"的数据项？').then(function () {
        return delAntenna(antennaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/antenna/export', {
        ...this.queryParams
      }, `antenna_${new Date().getTime()}.xlsx`)
    },
    getLocatedAddress(index) {
      const entries = this.dict.type.located_address;
      for(let i=0;i<entries.length;i++){
        if( entries[i].value == index){
            return entries[i].label
        }
      }
      return ""
    },
    getAntennaType(index) {
      const entries = this.dict.type.antenna_type;
      for(let i=0;i<entries.length;i++){
        if( entries[i].value == index){
            return entries[i].label
        }
      }
      return ""
    }
  }
};
</script>
